package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.dao.SiteMapper;
import com.bingo.entity.HisPool;
import com.bingo.entity.CurPool;
import com.bingo.entity.Site;
import com.bingo.entity.User;
import com.bingo.entity.dto.resp.RespSiteStats;
import com.bingo.service.HisPoolService;
import com.bingo.service.CurPoolService;
import com.bingo.service.SiteService;
import com.bingo.service.UserService;
import com.bingo.until.IsToday;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Bingo
 * @version 1.0
 * @description hai
 * @date 2024/3/21 19:29
 */
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements SiteService {
    private CurPoolService curPoolService;

    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }


    private HisPoolService hisPoolService;
    @Lazy
    @Autowired
    public void setPoolService(HisPoolService hisPoolService) {
        this.hisPoolService = hisPoolService;
    }

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public List<Site> getSiteBase() {
        //查询当前所以场
        QueryWrapper<Site> wrapper = new QueryWrapper<>();
        List<Site> list = this.list(wrapper);
        for (Site site : list) {
            QueryWrapper<CurPool> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("site_no", site.getSiteNo());
            List<CurPool> pools = curPoolService.list(queryWrapper);
            User user = userService.getUserByNo(site.getCustodianId());
            site.setName(user.getName());
            site.setPools(pools);
        }
        return list;
    }

    @Override
    public List<RespSiteStats> getSiteStats(List<String> siteNos, Date date) {
        //不提供场号则查询所有场
        if (siteNos == null || siteNos.size() == 0) {
            siteNos = this.getAllSiteNo();
        }

        LinkedList<RespSiteStats> list = new LinkedList<>();
        //判断是否当天
        if (IsToday.isToday(date)) {
            //遍历查询的场
            for (String siteNo : siteNos) {
                Site site = this.getSiteByNo(siteNo);
                if (site == null) {
                    continue;
                }

                //查询该场包含的所有塘
                QueryWrapper<CurPool> wrapper = new QueryWrapper<>();
                wrapper.eq("site_no", siteNo);
                List<CurPool> pools = curPoolService.list(wrapper);

                //初始化场的存塘情况
                RespSiteStats respSiteStats = new RespSiteStats();
                respSiteStats.setSiteNo(siteNo);
                respSiteStats.setArea(site.getArea());
                respSiteStats.setTotalNewWeight(0.0);
                respSiteStats.setTotalNewQuantity(0);
                respSiteStats.setTotalOldWeight(0.0);
                respSiteStats.setTotalOldQuantity(0);

                for (CurPool pool : pools) {
                    if (pool.getType() == 1) {
                        //新鳗
                        respSiteStats.setTotalNewQuantity(respSiteStats.getTotalNewQuantity() + pool.getQuantity());
                        respSiteStats.setTotalNewWeight(respSiteStats.getTotalNewWeight() + pool.getWeight());
                    } else {
                        respSiteStats.setTotalOldQuantity(respSiteStats.getTotalOldQuantity() + pool.getQuantity());
                        respSiteStats.setTotalOldWeight(respSiteStats.getTotalOldWeight() + pool.getWeight());
                    }
                }

                list.add(respSiteStats);

            }
        } else {
            for (String siteNo : siteNos) {
                Site site = this.getSiteByNo(siteNo);
                if (site == null) {
                    continue;
                }
                QueryWrapper<HisPool> wrapper = new QueryWrapper<>();
                wrapper.eq("site_no", siteNo);
                wrapper.eq("date", date);
                List<HisPool> hisPools = hisPoolService.list(wrapper);

                RespSiteStats respSiteStats = new RespSiteStats();
                respSiteStats.setSiteNo(siteNo);
                respSiteStats.setArea(site.getArea());
                respSiteStats.setTotalNewWeight(0.0);
                respSiteStats.setTotalNewQuantity(0);
                respSiteStats.setTotalOldWeight(0.0);
                respSiteStats.setTotalOldQuantity(0);

                for (HisPool hisPool : hisPools) {
                    if (hisPool.getType() == 1) {
                        respSiteStats.setTotalNewQuantity(respSiteStats.getTotalNewQuantity() + hisPool.getQuantity());
                        respSiteStats.setTotalNewWeight(respSiteStats.getTotalNewWeight() + hisPool.getWeight());
                    } else {
                        respSiteStats.setTotalOldQuantity(respSiteStats.getTotalOldQuantity() + hisPool.getQuantity());
                        respSiteStats.setTotalOldWeight(respSiteStats.getTotalOldWeight() + hisPool.getWeight());
                    }
                }
                list.add(respSiteStats);
            }
        }
        return list;
    }

    @Override
    public Site getSiteByNo(String siteNo) {
        QueryWrapper<Site> wrapper = new QueryWrapper<>();
        wrapper.eq("site_no", siteNo);
        Site site = this.getOne(wrapper);
        return site;
    }

    //只是返回塘号
    @Override
    public List<String> getPoolsBySiteNo(String siteNo) {
        QueryWrapper<CurPool> wrapper = new QueryWrapper<>();
        wrapper.eq("site_no", siteNo);
        List<CurPool> list = curPoolService.list(wrapper);
        LinkedList<String> poolNos = new LinkedList<>();
        for (CurPool pool : list) {
            poolNos.add(pool.getPoolNo());
        }
        return poolNos;
    }

    //返回所有场号
    @Override
    public List<String> getAllSiteNo() {
        List<Site> list = this.list();
        LinkedList<String> siteNos = new LinkedList<>();
        for (Site site : list) {
            siteNos.add(site.getSiteNo());
        }
        return siteNos;
    }
}
